package com.yeban.javase.review.exercise;

import java.io.*;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/27
 */
public class ObjectInputStreamReview {

    public static void main(String[] args){
        // 创建一个对象
        Student11 stu = new Student11("张三", 18);

        // 使用ObjectOutputStream将对象写入文件
        try (   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/com/yeban/javase/review/text/stu"))   ){
            // 写入
            oos.writeObject(stu);
            oos.flush();
            System.out.println("对象写入成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 读取对象
        try(    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src/com/yeban/javase/review/text/stu"))   ){
            Student11 s = (Student11)ois.readObject();
            System.out.println("读取成功：" + s);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

class Student11 implements Serializable {
    private String name;
    private int age;

    public Student11() {
    }

    public Student11(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Strudent{name = " + name + ", age = " + age + "}";
    }
}
